# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=gammaray
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}-qt5"
pkgver=2.11.3
pkgrel=3
arch=('any')
url="https://www.kdab.com/gammaray/"
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
pkgdesc="A tool for examining the internals of a Qt application and to some extent also manipulate it"
license=('GPL')
depends=(${MINGW_PACKAGE_PREFIX}-syntax-highlighting-qt5
         ${MINGW_PACKAGE_PREFIX}-qt5-tools
         ${MINGW_PACKAGE_PREFIX}-qt5-svg
         ${MINGW_PACKAGE_PREFIX}-qt5-3d
         ${MINGW_PACKAGE_PREFIX}-dlfcn)
makedepends=(${MINGW_PACKAGE_PREFIX}-cmake
             "${MINGW_PACKAGE_PREFIX}-cc"
             ${MINGW_PACKAGE_PREFIX}-ninja
             ${MINGW_PACKAGE_PREFIX}-pkg-config
             ${MINGW_PACKAGE_PREFIX}-extra-cmake-modules
             ${MINGW_PACKAGE_PREFIX}-kcoreaddons-qt5
             ${MINGW_PACKAGE_PREFIX}-kdstatemachineeditor-qt5
             ${MINGW_PACKAGE_PREFIX}-qt5-script
             ${MINGW_PACKAGE_PREFIX}-qtwebkit
             ${MINGW_PACKAGE_PREFIX}-qt5-scxml
             ${MINGW_PACKAGE_PREFIX}-qt5-location
             ${MINGW_PACKAGE_PREFIX}-qt5-connectivity
             ${MINGW_PACKAGE_PREFIX}-doxygen
             ${MINGW_PACKAGE_PREFIX}-glslang)
optdepends=("${MINGW_PACKAGE_PREFIX}-qtwebkit: web inspector plugin"
            "${MINGW_PACKAGE_PREFIX}-qt5-scxml: state machine viewer plugin"
            "${MINGW_PACKAGE_PREFIX}-qt5-connectivity: bluetooth plugin"
            "${MINGW_PACKAGE_PREFIX}-qt5-script: script engine debugger plugin"
            "${MINGW_PACKAGE_PREFIX}-kcoreaddons-qt5: KJob tracker plugin"
            "${MINGW_PACKAGE_PREFIX}-kdstatemachineeditor-qt5: ")
source=(https://github.com/KDAB/GammaRay/releases/download/v${pkgver}/${_realname}-${pkgver}.tar.gz
        001-use-webkit.patch
        003-win32-not-elf.patch
        004-files-layout.patch
        005-fix-dll-naming.patch)
noextract=(${_realname}-${pkgver}.tar.gz)
sha256sums=('03d7ca7bd5eb600c9c389d0cf071960330592f1f392a783b7fec5f9eaa5df586'
            'd85b7e94803a1ebda912d7ac1b1b77380551a452c2ebbdb4ff03f8b140266887'
            'cff9bda2dbde8054bbaccba52a739d59d99808c3f5e9fcdd13747779c5003787'
            '86a4e90eb77ed2c04f4cb3fdffef06b0c69915cd9870de76c81ade5562adfea6'
            '1518267e0c1231cfc8886d244ceb3f1865e95bad036e046816ac7d511a3c36b3')
validpgpkeys=(E86C000370B1B9E2A9191AD53DBFB6882C9358FB) # KDAB Products <info@kdab.com>

prepare() {
  [[ -d ${srcdir}/${_realname}-${pkgver} ]] && rm -rf ${srcdir}/${_realname}-${pkgver}
  tar -xf ${srcdir}/${_realname}-${pkgver}.tar.gz -C "${srcdir}" || true

  mkdir -p build-${CARCH}

  cd ${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/001-use-webkit.patch
  patch -p1 -i ${srcdir}/003-win32-not-elf.patch
  patch -p1 -i ${srcdir}/004-files-layout.patch
  patch -p1 -i ${srcdir}/005-fix-dll-naming.patch
}

build() {
  local ABSOLUTE_PREFIX=$(cygpath -am ${MINGW_PREFIX})
  declare -a extra_conf
  case ${MSYSTEM} in
    CLANG*)
      extra_conf+=("-DSTACK_DETAILS_BFD=OFF")
      ;;
    *)
      extra_conf+=("-DSTACK_DETAILS_BFD=ON")
      ;;
  esac
  
  cd build-${CARCH}

  LDFLAGS+=" -L$(cygpath -am ${MINGW_PREFIX}/lib/binutils)"
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=;-DECM_MKSPECS_INSTALL_DIR=;-DPLUGIN_INSTALL_DIR=;" \
  ${MINGW_PREFIX}/bin/cmake.exe \
      -G "Ninja" \
      -DCMAKE_BUILD_TYPE=RelWithDebInfo \
      -DCMAKE_INSTALL_OLDINCLUDEDIR=${MINGW_PREFIX}/include \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DGAMMARAY_INSTALL_QT_LAYOUT=ON \
      -DTRANSLATION_INSTALL_DIR=${MINGW_PREFIX}/share/qt5/translations \
      -DPLUGIN_INSTALL_DIR=${MINGW_PREFIX}/share/qt5/plugins/gammaray \
      -DECM_MKSPECS_INSTALL_DIR=${MINGW_PREFIX}/share/qt5/mkspecs/modules \
      -DGAMMARAY_MULTI_BUILD=FALSE \
      -DSTACK_DETAILS_AUTO_DETECT=OFF \
      ${extra_conf} \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}

package() {
  cd build-${CARCH}
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --build ./ --target install
}
